package com.itheima.crm.controller;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.itheima.crm.pojo.RiZhi;
import com.itheima.crm.pojo.YuanGong;
import com.itheima.crm.service.RiZhiService;
import com.itheima.crm.service.YuanGongService;

/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class RegisterController {

	
	@Autowired
	private YuanGongService yuangongService;
	@Autowired
	private RiZhiService rizhiService;
	
	// 注解在成员变量上

	public YuanGongService getYuangongService() {
		return yuangongService;
	}

	public void setYuangongService(YuanGongService yuangongService) {
		this.yuangongService = yuangongService;
	}

	public RiZhiService getRizhiService() {
		return rizhiService;
	}

	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}

	@RequestMapping(value = "/register")
	public ModelAndView register(Model model) {
		ModelAndView a = new ModelAndView();
		a.setViewName("register");

		return a;

	}

	@RequestMapping(value = "/tologin")
	public ModelAndView tologin(Model model) {
		ModelAndView a = new ModelAndView();
		a.setViewName("login");

		return a;

	}

	@RequestMapping(value = "/toGuanLi")
	public ModelAndView toGuanLi(Model model) {
		ModelAndView a = new ModelAndView();
		a.setViewName("GuanLilogin");

		return a;

	}

	@RequestMapping(value = "/toregister")
	public ModelAndView toregister(Model model) {
		ModelAndView a = new ModelAndView();
		a.setViewName("register");

		return a;

	}

	@RequestMapping(value = "/register.action")
	public String register(YuanGong yuangong) {
		RiZhi a=new RiZhi();
		a.setYgid(yuangong.getYgid());
		a.setYgname(yuangong.getYgname());
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("注册员工系统");
		rizhiService.insertApplication(a);

		yuangongService.updatesRegisterMessage(yuangong);

		return "login";

	}

	
	@RequestMapping(value = "/login.action")
	public ModelAndView login(YuanGong yuangong, Model model, HttpServletRequest request) {

		YuanGong result = yuangongService.selectLogin(yuangong);

		if (yuangong.getLoginname().equals(result.getLoginname())
				&& yuangong.getLoginpw().equals(result.getLoginpw())) {
			System.out.println("相同");
			
			ModelAndView a = new ModelAndView("ApplicationFind");
			
			request.getSession().setAttribute("loginname", yuangong.getLoginname());
			request.getSession().setAttribute("loginpw", yuangong.getLoginpw());
			request.getSession().setAttribute("ygname", result.getYgname());
			request.getSession().setAttribute("ygid", result.getYgid());
			
			
			if(result.getZhiwei().equals("2")) {
				result.setZhiwei("2");
				
			}

			if(result.getZhiwei().equals("1")) {
				result.setZhiwei("1");
				
			}
			request.getSession().setAttribute("zhiwei", result.getZhiwei());
			request.getSession().setAttribute("organziation", result.getOrganization());
			
			a.addObject("result", result);
			
			return a;

		}
	
		ModelAndView a = new ModelAndView("login");

		return a;

	}
	
	@RequestMapping(value = "/GuanLi.action")
	public ModelAndView GuanLilogin(YuanGong yuangong, Model model, HttpServletRequest request) {

		YuanGong result = yuangongService.selectLogin(yuangong);

		if (yuangong.getLoginname().equals(result.getLoginname())
				&& yuangong.getLoginpw().equals(result.getLoginpw())) {
			System.out.println("相同");
			
			ModelAndView a = new ModelAndView("ApplicationFind");
			
			request.getSession().setAttribute("loginname", yuangong.getLoginname());
			request.getSession().setAttribute("loginpw", yuangong.getLoginpw());
			request.getSession().setAttribute("ygname", result.getYgname());
			request.getSession().setAttribute("ygid", result.getYgid());
			
			
			if(result.getZhiwei().equals("3")) {
				result.setZhiwei("3");
				
			}
			request.getSession().setAttribute("zhiwei", result.getZhiwei());
			request.getSession().setAttribute("organziation", result.getOrganization());
			
			a.addObject("result", result);
			
			return a;

		}
	
		ModelAndView a = new ModelAndView("GuanLilogin");

		return a;

	}

}